home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / parallax / ibm_pc_d.exe / SAMPLES / REALPIC.P < prev    next >
Text File  |  1992-11-06  |  29KB  |  877 lines

  1. SYSTEM Evaluation_of_real_Random_Stereo_Images ;             (* Version 1.0 *)
  2.  
  3. TYPE
  4.  string            = ARRAY[ 200 ] OF CHAR ;
  5.  
  6. CONST 
  7.  maxWidth          =  64 ;     (* maximal Width of input image              *)
  8.  maxHeigth         =  64 ;     (* maximal Heigth of input image             *)
  9.  maxDepth          =   8 ;     (* depth of computation                      *)
  10.  margin            =   2 ; 
  11.  Filter_iterat =   2 ;
  12.  
  13. CONFIGURATION 
  14.  picture [ 1 .. maxHeigth ] , [ 1 .. maxWidth ] ;
  15.  
  16. CONNECTION    
  17.  left_port : picture[ i,j ] <-> picture[ i  ,j-1 ].right_port ;
  18.  up_port   : picture[ i,j ] <-> picture[ i-1,j   ].down_port ;
  19.  
  20. SCALAR
  21.  LeftPicture ,
  22.  RightPicture    : ARRAY [ 1 .. maxHeigth ] , [ 1 .. maxWidth ] OF INTEGER ;
  23.                                (* memory for left and right image           *)
  24.  
  25.  i , j ,                       (* counter                                   *)
  26.  PicHeigth,                    (* Heigth of Inputimage                      *)
  27.  PicWidth        : INTEGER ;   (* Width of Inputimage                       *)
  28.  
  29.  OutputFile,
  30.  InputFile,
  31.  FileName        : string ;    (* name of Inputfile                         *)
  32.  Batch           : BOOLEAN ;   (* flag for Batchmode                        *)
  33.  counter,                      (* for Batchmode necessary variables         *)
  34.  Number_of_Lines,
  35.  RegionX,
  36.  RegionY        : INTEGER ;           
  37.  
  38. VECTOR
  39.  Pixel           : ARRAY [ 1 .. maxDepth ] OF INTEGER ;  
  40.                                (* memory for each Image-plane               *)
  41.  
  42.  Heigth,                       (* to which plain belongs a pixel            *)
  43.  Left,                         (* Pixel of left image                       *)
  44.  Right : INTEGER ;             (* Pixel of right image                      *)
  45.            
  46.  
  47. (************************* STRCAT *******************************************)
  48. (* catenation of two strings                                                *)
  49.  
  50. PROCEDURE strcat( SCALAR first , second : string ) : SCALAR string ;
  51.  
  52.  SCALAR
  53.   i , j : INTEGER ;
  54.  
  55.  BEGIN          
  56.   i := 0 ; j := 0 ;
  57.   WHILE first[i]  <> CHR(0) DO INC(i) ; END ;
  58.   WHILE second[j] <> CHR(0) DO 
  59.     first[i] := second[j] ; 
  60.     INC(j) ;INC(i) ; 
  61.   END ;
  62.   first[i] := CHR(0) ; 
  63.  
  64.   RETURN first ;
  65.  END strcat ;
  66.      
  67. (************************** READPICTURE *************************************)
  68. (* request Imagename, check Imagesize, possibly request Imagesection read   *)
  69. (* Imagedata into the scalar array's LeftPicture[][] and RightPicture[][].  *)
  70.  
  71. PROCEDURE ReadPicture() : SCALAR BOOLEAN ;
  72.             
  73.  SCALAR               
  74.   Character      : CHAR ;
  75.   Heigth, Width  : INTEGER ;
  76.   XPos, YPos     : INTEGER ;
  77.   DeltaX, DeltaY : INTEGER ;
  78.   magic          : string ;
  79.   color          : INTEGER ;
  80.  
  81.  BEGIN
  82.   PicHeigth := maxHeigth  ; 
  83.   PicWidth  := maxWidth ;
  84.        
  85.   IF NOT Batch THEN
  86.    WriteString( "Inputfile (without extension '.l.ppm' and '.r.ppm' ) : " ) ; 
  87.                               (* requesting Filename *)
  88.    ReadString( InputFile ) ;
  89.   END ;
  90.  
  91.   FileName := strcat( InputFile , ".l.ppm" ) ;
  92.   OpenInput( FileName ) ;     (* open File *)
  93.   IF Done THEN
  94.     ReadString( magic ) ;
  95.     ReadInt( Width ) ;        (* reading Heigth and Width of image *)
  96.     ReadInt( Heigth ) ;
  97.     CloseInput ;
  98.  
  99.     IF Heigth < PicHeigth THEN
  100.      PicHeigth := Heigth ;
  101.     END ; (* IF Heigth *)
  102.     
  103.     IF Width < PicWidth THEN
  104.      PicWidth := Width ;
  105.     END ; (* IF Heigth *)
  106.         
  107.     IF ( Heigth > PicHeigth ) OR ( Width > PicWidth ) THEN
  108.      IF NOT Batch THEN
  109.        WriteString( "Image too large. Please enter Image-Region : " ) ;
  110.        ReadInt( DeltaX ) ;
  111.       ELSE
  112.        DeltaX := RegionX ;
  113.        DeltaY := RegionY ;
  114.      END ; (* IF *)
  115.  
  116.      IF DeltaX >= 0 THEN
  117.        IF NOT Batch THEN
  118.     WriteString( "Y-Verschiebung : " ) ;
  119.     ReadInt( DeltaY ) ;
  120.        END ; (* IF *)
  121.        DeltaY := ABS( DeltaY ) ;
  122.        IF ( Width - PicWidth ) < DeltaX THEN
  123.     DeltaX :=  Width - PicWidth ;
  124.        END ;
  125.        IF ( Heigth - PicHeigth ) < DeltaY THEN
  126.     DeltaY :=  Heigth - PicHeigth ;
  127.        END ;
  128.       ELSE                          (* Delta negativ : using default region *)
  129.        CASE ABS( DeltaX ) OF
  130.      1 :  DeltaX := 0                         ; DeltaY :=  Heigth - PicHeigth ; |
  131.      2 :  DeltaX := (Width - PicWidth ) DIV 2 ; DeltaY :=  Heigth - PicHeigth ; |
  132.      3 :  DeltaX :=  Width - PicWidth         ; DeltaY :=  Heigth - PicHeigth ; |
  133.      4 :  DeltaX := 0                         ; DeltaY := (Heigth - PicHeigth) DIV 2 ; |
  134.      5 :  DeltaX := (Width - PicWidth ) DIV 2 ; DeltaY := (Heigth - PicHeigth) DIV 2 ; |
  135.      6 :  DeltaX :=  Width - PicWidth         ; DeltaY := (Heigth - PicHeigth) DIV 2 ; |
  136.      7 :  DeltaX := 0                         ; DeltaY := 0 ; |
  137.      8 :  DeltaX := (Width - PicWidth ) DIV 2 ; DeltaY := 0 ; |
  138.      9 :  DeltaX :=  Width - PicWidth         ; DeltaY := 0 ;
  139.     ELSE
  140.      WriteString("Incorrect region.") ; WriteLn ;
  141.      RETURN FALSE ;
  142.        END ; (* CASE ABS(DeltaX) *)
  143.      END ; (* IF DeltaX >= 0 *) 
  144.     ELSE
  145.      DeltaX := 0 ; DeltaY := 0 ;
  146.     END ; (* IF (Heigth.. *)
  147.         
  148.     OpenInput( FileName ) ;
  149.  
  150.     ReadString( magic ) ;
  151.     ReadInt( Width ) ;  Read( Character ) ;                (* read Linefeed *)
  152.     ReadInt( Heigth ) ; Read( Character ) ;                (* read Linefeed *)
  153.     ReadInt( color ) ; Read( Character ) ;
  154.  
  155.     WriteString( "Reading left image ..." ) ; WriteLn ;
  156.  
  157.     FOR YPos := 1 TO Heigth DO
  158.      FOR XPos := 1 TO Width DO
  159.       Read( Character ) ;
  160.       Read( Character ) ;
  161.       Read( Character ) ;
  162.       IF (DeltaY < YPos <= (DeltaY+PicHeigth)) AND    (* check image region *)
  163.      (DeltaX < XPos <= (DeltaX+PicWidth)) THEN
  164.     LeftPicture[ YPos-DeltaY ][ XPos-DeltaX ] := ORD( Character ) ;
  165.       END ; (* IF *)
  166.      END ; (* FOR *)
  167.     END ; (* FOR *) 
  168.  
  169.     FileName := strcat( InputFile , ".r.ppm" ) ;
  170.     OpenInput( FileName ) ;
  171.  
  172.     ReadString( magic ) ;
  173.     ReadInt( Width )  ; Read( Character ) ;                (* read Linefeed *)
  174.     ReadInt( Heigth ) ; Read( Character ) ;                (* read Linefeed *)
  175.     ReadInt( color ) ; Read( Character ) ;
  176.  
  177.     WriteString( "Reading right image ..." ) ; WriteLn ;
  178.  
  179.     FOR YPos := 1 TO Heigth DO
  180.      FOR XPos := 1 TO Width DO
  181.       Read( Character ) ;
  182.       Read( Character ) ;
  183.       Read( Character ) ;
  184.       IF (DeltaY < YPos <= (DeltaY+PicHeigth)) AND    (* check image region *)
  185.      (DeltaX < XPos <= (DeltaX+PicWidth)) THEN
  186.     RightPicture[ YPos-DeltaY ][ XPos-DeltaX ] := ORD( Character ) ;
  187.       END ; (* IF *) 
  188.      END ; (* FOR *)
  189.     END ; (* FOR *)
  190.  
  191.     RETURN TRUE ;
  192.  
  193.    ELSE            
  194.     WriteString( "File not found." ) ; WriteLn ;
  195.     RETURN FALSE ;
  196.   END ;(* IF Done *)
  197.  END ReadPicture ;
  198.  
  199. (************************** Show_Plains *************************************)
  200. (* This procedure display the result of each image-plain for possible       *)
  201. (* viewing after each calculating step                                      *)
  202.  
  203. PROCEDURE Show_Plains ;
  204.  
  205.  SCALAR
  206.   plain, x, y : INTEGER ;
  207.   PicPlain    : ARRAY [ 1 .. maxHeigth ] , [ 1 .. maxWidth ] OF CHAR ; 
  208.                            (* memory of image-plain *)
  209.       
  210.  VECTOR
  211.   picture_element : CHAR ;
  212.  
  213.  BEGIN
  214.   IF NOT Batch THEN                (* only for interactive mode *)
  215.    REPEAT           
  216.     WriteString("Which plain do you want to see ? ( 1 .. ");
  217.     WriteInt( maxDepth , 1 ) ;
  218.     WriteString(" , 0 = no plain ) : ") ;
  219.     ReadInt( plain ) ;
  220.     IF 1 <= plain <= maxDepth THEN  
  221.      PARALLEL
  222.        picture_element := CHR (0);
  223.      ENDPARALLEL;
  224.  
  225.      PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ] 
  226.       picture_element := CHR( Pixel[ plain ]+ORD('0') );
  227.      ENDPARALLEL ; 
  228.  
  229.      STORE( picture_element , PicPlain ) ;
  230.      WriteString("Image-Plain : ") ; WriteInt( plain , 1 ) ; WriteLn ;
  231.      FOR y := 1 TO PicHeigth DO
  232.       WriteString( PicPlain[y] ) ;
  233.       WriteLn ;
  234.      END ; (* FOR y *)
  235.      WriteLn ;  
  236.     END ; (* IF 1 <= plain .. *)
  237.    UNTIL plain = 0 ;
  238.   END ; (* IF NOT Batch *)
  239.  END Show_Plains ;
  240.          
  241. (************************* Compute_Heigth ***********************************)
  242. (* Computes the plain-number on which a pixel lies                          *)
  243.  
  244. PROCEDURE Compute_Heigth ;
  245.  
  246.  SCALAR
  247.    plain  : INTEGER ;
  248.  
  249.  BEGIN
  250.   PARALLEL
  251.     Heigth := 0 ;
  252.   ENDPARALLEL;
  253.  
  254.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ] 
  255.     FOR plain := 1 TO maxDepth DO  (* to which plain belongs a Pixel *)
  256.     IF Pixel[plain] = 1 THEN
  257.      Heigth := plain ;
  258.     END ; (* IF Pixel[ plain ] = 1 *)
  259.    END ; (* FOR *)
  260.   ENDPARALLEL ;
  261.  END Compute_Heigth ;
  262.  
  263. (************************* Show_All *****************************************)
  264. (* This procedure display the results of all image-plains                   *)
  265.  
  266. PROCEDURE Show_All ;
  267.  
  268.  SCALAR
  269.   x, y   : INTEGER ;
  270.   Picture   : ARRAY [ 1 .. maxHeigth ] , [ 1 .. maxWidth ] OF CHAR ; 
  271.                                        (* Memory of the whole Image *)
  272.          
  273.  VECTOR
  274.    picture_element : CHAR ;
  275.  
  276.  BEGIN
  277.   IF NOT Batch THEN
  278.    PARALLEL
  279.      picture_element := CHR (0);
  280.    ENDPARALLEL;
  281.  
  282.    PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ] 
  283.     picture_element := CHR( ORD('0') + Heigth ) ;
  284.    ENDPARALLEL ;
  285.  
  286.    STORE( picture_element , Picture ) ;
  287.    FOR y := 1 TO PicHeigth DO
  288.     WriteString( Picture[y] ) ;
  289.     WriteLn ;
  290.    END ; (* FOR y *)
  291.    WriteLn ;  
  292.   END ; (* IF NOT Batch *)
  293.  END Show_All ;
  294.  
  295. (************************* PPM_Output ***************************************)
  296. (* saving the resulting heigth-information, File Format is PPM              *)
  297.  
  298. PROCEDURE PPM_Output ;
  299.  
  300.  SCALAR
  301.   i, j , color,a,b  : INTEGER ;
  302.  
  303.  BEGIN
  304.   PARALLEL
  305.     STORE( Heigth , LeftPicture ); 
  306.     color := REDUCE.MAX(Heigth);
  307.   ENDPARALLEL;
  308.  
  309.  
  310.  
  311.   OpenOutput( FileName ) ;
  312.   WriteString ( "P6" ) ; WriteLn ;              (* RAWBIT option, not ASCII *)
  313.  
  314.   WriteInt( PicWidth(*-2*margin*) , 1 ) ; WriteLn ; (* without vertical margins *)
  315.   WriteInt( PicHeigth(*-2*margin*)  , 1 ) ; WriteLn ; (* without horizontal margins *)  
  316.  
  317.   WriteInt( color, 1) ; WriteLn ;          (* maximum color-component value *)
  318.   
  319.   FOR i := 1(*+margin*) TO PicHeigth(*-margin*) DO
  320.    FOR j := 1(*+margin*) TO PicWidth(*-margin*) DO      (* saving pixel: *)
  321.     Write( CHR( LeftPicture[i,j] )) ;           (* red value     *)
  322.     Write( CHR( LeftPicture[i,j] )) ;           (* green value   *)
  323.     Write( CHR( LeftPicture[i,j] )) ;           (* blue value    *)
  324.   
  325.    END ; (* FOR j *)
  326.   END ; (* FOR i *)
  327.   CloseOutput ;
  328.   
  329.  END PPM_Output ;
  330.  
  331. (************************* Search_Edge **************************************)
  332. (* This procedure replace the original-pictures with their edge-pictures.   *)
  333. (* The calculation rule:                                                    *)
  334. (* EDGE := SQRT( (LB-RB)^2 + (DB-UB)^2 ), with LB,RB,DB and UB              *)
  335. (* representing the brightness left, right, up and down                     *)
  336.  
  337. PROCEDURE Search_Edge( VECTOR Pixel : INTEGER ) : VECTOR INTEGER ;  
  338.  
  339.  SCALAR
  340.   Minimum,
  341.   Maximum   : INTEGER ;
  342.  
  343.  VECTOR
  344.   Sum1, 
  345.   Sum2,
  346.   Help,
  347.   Neighbour : INTEGER ;
  348.   Factor    : REAL ;
  349.  
  350.  BEGIN  
  351.   WriteString("Computing edge-pictures ...") ; WriteLn ;
  352.  
  353.   PROPAGATE.right_port( Pixel , Neighbour ) ; 
  354.                                      (* get brightness from above and below *)
  355.   Sum1 := Neighbour + Pixel ;
  356.   PROPAGATE.left_port( Pixel , Neighbour ) ;
  357.   Sum1 := Sum1 + Neighbour ;
  358.  
  359.   PROPAGATE.up_port( Sum1 , Sum2 ) ;
  360.   PROPAGATE.down_port( Sum1 ) ;
  361.     
  362.   Help := (Sum1-Sum2)**2 ;
  363.  
  364.   PROPAGATE.up_port( Pixel , Neighbour ) ;  
  365.                                       (* get brightness from left and right *)
  366.   Sum1 := Neighbour + Pixel ;
  367.   PROPAGATE.down_port( Pixel , Neighbour ) ;
  368.   Sum1 := Sum1 + Neighbour ;
  369.  
  370.   PROPAGATE.left_port( Sum1 , Sum2 ) ;
  371.   PROPAGATE.right_port( Sum1 ) ;
  372.  
  373.   Pixel := TRUNC( Sqrt(FLOAT(Help + (Sum1-Sum2)**2)) ) ; 
  374.               (* replacing orginal-picture with edge-picture *)
  375.   Minimum := REDUCE.MIN(Pixel) ;
  376.   Maximum := REDUCE.MAX(Pixel) ;
  377.  
  378.   Factor := 16.0 / FLOAT( Maximum - Minimum ) ;     (* grade of compression *)
  379.   Pixel := TRUNC(FLOAT(Pixel - Minimum) * Factor) ;
  380.   IF Pixel < 3 THEN
  381.    Pixel := 0 ;
  382.   END ;
  383.   RETURN Pixel ;
  384.  END Search_Edge ;
  385.  
  386.  
  387. (************************** ParallelSum3x3 **********************************)
  388. (* ParallelSum3x3 calculates the sum of certain elements in a 3x3           *)
  389. (* size neighbourhood                                                       *)
  390.  
  391. PROCEDURE ParallelSum3x3( VECTOR my_number : INTEGER ) : VECTOR INTEGER ;
  392.  
  393.  VECTOR  
  394.   Sum,
  395.   shift_number : INTEGER ;     (* number which is sending to the neighbours *)
  396.  
  397.  BEGIN
  398.   Sum := my_number ;           (* number of myself *)
  399.  
  400.   PROPAGATE .right_port(my_number,shift_number) ; 
  401.                                (* sending pixelvalue to the right *)
  402.   Sum := Sum + shift_number ;             
  403.   PROPAGATE .left_port(my_number,shift_number) ;  
  404.                                (* sending to the left *)
  405.   Sum := Sum + shift_number ;    
  406.   my_number := Sum ;           (* store calculated horizontal sum *)
  407.  
  408.   PROPAGATE .up_port(my_number,shift_number) ;   
  409.                                (* sending intermediate result above *)
  410.   Sum := Sum + shift_number ;  
  411.           
  412.   PROPAGATE .down_port(my_number,shift_number) ;  
  413.                                (* sending below *)
  414.   Sum := Sum + shift_number ;    
  415.   
  416.   RETURN Sum ;                 (* return the calculating sum *)
  417.  END ParallelSum3x3 ;
  418.  
  419. (************************** Average *****************************************)
  420. (* Calculates the average of a value (my_number) within a range specified   *)
  421. (* by expansion. There will be considered only values greater than zero.    *)
  422.  
  423. PROCEDURE Average( VECTOR my_number : REAL ;
  424.                    SCALAR Umgebung  : INTEGER ) : VECTOR REAL ;
  425.  
  426.  SCALAR
  427.   i             : INTEGER ;      (* counter *)
  428.      
  429.  VECTOR        
  430.   Sum_Count,                     (* counter for valid values *)
  431.   Count1, 
  432.   Count2        : INTEGER ;
  433.   Sum,                           (* Sum in the region *)    
  434.   shift_number1,
  435.   shift_number2 : REAL    ;      (* numbers for sending to the neighbours *)
  436.  
  437.  BEGIN
  438.   shift_number1 := my_number ;   
  439.   shift_number2 := my_number ;
  440.   Sum           := my_number ;   (* number of myself *)
  441.  
  442.   Count1 := 0 ; Count2 := 0 ;  
  443.   IF my_number > 0.0 THEN        (* counting only values > 0.0 *)
  444.    Count1 := 1 ; Count2 := 1 ;
  445.   END ;
  446.   Sum_Count := Count1 ;
  447.  
  448.   FOR i := 1 TO Umgebung DO                  
  449.    PROPAGATE .right_port(shift_number1) ;   (* sending own pixelvalue to the  *)
  450.    PROPAGATE .left_port (shift_number2) ;   (* right and to the left *)
  451.    Sum := Sum + shift_number1 + shift_number2 ; 
  452.  
  453.    PROPAGATE .right_port(Count1) ;   (* informing about validity of the values *)
  454.    PROPAGATE .left_port (Count2) ;
  455.    Sum_Count := Sum_Count + Count1 + Count2 ;
  456.   END ;
  457.  
  458.   shift_number1 := Sum ;  (* store the calculated horizontal sum *)
  459.   shift_number2 := Sum ;  (* and sending above and below *)
  460.   Count1 := Sum_Count ;
  461.   Count2 := Sum_Count ;
  462.  
  463.   FOR i := 1 TO Umgebung DO                  
  464.    PROPAGATE .up_port  (shift_number1) ;     (* sending own pixelvalue to the  *)
  465.    PROPAGATE .down_port(shift_number2) ;     (* right and to the left *)
  466.    Sum := Sum + shift_number1 + shift_number2 ; (* and add *)
  467.  
  468.    PROPAGATE .up_port  (Count1) ;   (* informing about validity of the values *)
  469.    PROPAGATE .down_port(Count2) ;
  470.    Sum_Count := Sum_Count + Count1 + Count2 ;
  471.   END ;
  472.  
  473.   IF Sum_Count > 0 THEN
  474.     Sum := Sum / FLOAT(Sum_Count) ;
  475.   END ; (* IF Sum_Count > 0  *)
  476.             
  477.   RETURN Sum ;
  478.  END Average ;
  479.  
  480. (************************** Common_Elements *********************************)
  481. (* The vectors LEFT and RIGHT contains in the beginning both images in the  *)
  482. (* right position against one another. They would be compared pixelwise and *)
  483. (* the result stored in the vector PIXEL[].                                 *)
  484. (* Afterwards the left image would be shifted left by one pixel and compared*)
  485. (* again for common elements with the right image. This repeats until       *)
  486. (* maxDepth is reached.                                                     *)
  487.  
  488. PROCEDURE Common_Elements ;
  489.  
  490.  SCALAR
  491.    plain  : INTEGER ;
  492.  
  493.  BEGIN
  494.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  495.    FOR plain := 1 TO maxDepth DO         (* for each image-plain *)
  496.     Pixel[plain] := 0 ;
  497.     IF (Left # 0) & (Right # 0) THEN
  498.       Pixel[plain] := 1 + ABS( Left-Right) ;
  499.     END ; (* IF (Left *)
  500.     
  501.     PROPAGATE .left_port(Left) ; 
  502.                            (* shifting left image one position to the left *)
  503.    END ;     
  504.   ENDPARALLEL ;          
  505.  END Common_Elements ;
  506.     
  507. (************************** Search_Objects **********************************)
  508. (* For almost each pixel it exists some possible plains. By comparing with  *)
  509. (* neighbour-pixels it can be find out to which plain a pixel belongs.      *)
  510.  
  511. PROCEDURE Search_Objects ;
  512.  
  513.  SCALAR
  514.   plain       : INTEGER ;
  515.  
  516.  VECTOR                               
  517.   minCount,
  518.   minPos      : INTEGER ;
  519.   min, 
  520.   PixAverage : REAL ;
  521.  
  522.  BEGIN
  523.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  524.    min :=100000.0 ; minPos := 0 ; minCount := 0 ;
  525.    FOR plain := 1 TO maxDepth DO          (* for each plain                 *)
  526.     PixAverage := Average( FLOAT( Pixel[ plain ]), margin ) ;
  527.     IF Pixel[plain] <> 0 THEN      (* only if plain is valid                *)
  528.      Pixel[plain] := 0 ;           (* first make plain invalid              *)
  529.  
  530.      IF PixAverage = min THEN      (* if same minimum found                 *)
  531.       INC( minCount ) ;            (* increase numbers of found mimima      *)
  532.      END ; (* PixAverage = min *)
  533.  
  534.      IF PixAverage < min THEN      (* if new miminum found                  *)
  535.       minPos   := plain ;          (* make this plain valid                 *)
  536.       min      := PixAverage ;     (* store minimum                         *)
  537.       minCount := 1 ;              (* set numbers of found minima to 1      *)
  538.      END ; (* IF PixAverage < min  *)
  539.     END ; (* IF Pixel[plain] <> 0 *)
  540.    END ; (* FOR plain *)
  541.  
  542.    IF (minPos > 0) AND (minCount = 1) THEN   (* admit only clear minima     *)
  543.     Pixel[minPos] := 1 ;           (* minPos is the valid plain             *)
  544.    END ;
  545.   ENDPARALLEL ;
  546.  END Search_Objects ;
  547.  
  548.  
  549. (************************* Filter *******************************************)
  550. (* It's possible that some few pixel would not be correct calculated.       *)
  551. (* The procedure Filter has to search for these pixels and then assign      *)
  552. (* to a suitable plain.                                                     *)
  553.  
  554. PROCEDURE Filter ;
  555.  
  556.  SCALAR
  557.    tolerance,
  558.    plain,
  559.    num_changes  : INTEGER ;
  560.  
  561.  VECTOR                
  562.    previous,
  563.    max, 
  564.    maxpos, 
  565.    PixelSum : INTEGER ;
  566.    change     : BOOLEAN ;
  567.  
  568.  BEGIN
  569.   WriteString("Filter ...") ; WriteLn ;
  570.   tolerance := Filter_iterat;
  571.  
  572.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  573.    REPEAT
  574.     max := 0 ; change := FALSE ;
  575.     FOR plain := 1 TO maxDepth DO   (* for each image-plain *)
  576.      PixelSum := ParallelSum3x3( Pixel[plain] ) ; 
  577.                                     (* look at 3x3-region neighbourhood *)
  578.      IF PixelSum > max THEN
  579.        max    := PixelSum ;         (* store the propably best plain *)
  580.        maxpos := plain ;            
  581.      END ; (* IF PixelSum *)
  582.  
  583.      IF ( Pixel[plain] = 1 ) AND ( PixelSum <= tolerance ) THEN 
  584.                                     (* 'lost pixel' ? *)
  585.       Pixel[plain] := 0 ;
  586.       change := TRUE ;
  587.       previous := plain ;
  588.      END ; (* IF Pixel[plain] *)
  589.     END ; (* FOR plain *)
  590.  
  591.     IF change AND (max > 1) AND (previous <> maxpos) THEN
  592.       Pixel[maxpos] := 1 ;          (* 'lost pixel' get a new plain *)
  593.     END ; (* IF change AND *)
  594.  
  595.     num_changes := REDUCE.SUM( ORD(change) ) ;
  596.     DEC( tolerance ) ;
  597.     WriteString("Changes : ") ; WriteInt( num_changes , 3 ) ; WriteLn ;
  598.    UNTIL (num_changes = 0) OR (tolerance = 0) ;
  599.   ENDPARALLEL ;
  600.  END Filter ;
  601.  
  602. (**************** Interpolate_and_Heigthlines  ******************************)
  603. (* Interpolate intervalls                                                   *)
  604.  
  605. PROCEDURE Interpolate_and_Heigthlines ;
  606.  
  607.  SCALAR  
  608.   num_Linien,               (* number of heigth-lines which have to         *)
  609.                             (* be draw in                                   *)
  610.   num_changes,              (* number of executed changes                   *)
  611.   j            : INTEGER ;  (* loop-variable                                *)
  612.   min_Value,                (* minimal heigth-value                         *)
  613.   max_Value,                (* maximal heigth-value                         *)
  614.   step : REAL ;
  615.  
  616.  VECTOR  
  617.   change,               
  618.   fix          : BOOLEAN ;  (* fix = TRUE => not allowed to change pixel    *)
  619.   previous,                 (* value of previous iteration                  *)
  620.   RHeigth,                  (* Heigth as real-number                        *)
  621.   medium       : REAL ;
  622.  
  623.  (*-------------------------- skip_plain -----------------------------------*)
  624.  
  625.  PROCEDURE skip_plain( SCALAR skipvalue : REAL ) : VECTOR BOOLEAN ;  
  626.  
  627.   VECTOR
  628.    skip     : BOOLEAN ;
  629.    Neighbour1,
  630.    Neighbour2,
  631.    Neighbour3,
  632.    Neighbour4   : REAL    ;
  633.  
  634.   BEGIN  
  635.    skip := FALSE ;
  636.    PROPAGATE.left_port ( RHeigth , Neighbour1 ) ;
  637.    PROPAGATE.right_port( RHeigth , Neighbour2 ) ;
  638.    PROPAGATE.up_port   ( RHeigth , Neighbour3 ) ;
  639.    PROPAGATE.down_port ( RHeigth , Neighbour4 ) ;
  640.    IF ( RHeigth <= skipvalue < Neighbour1 ) OR
  641.       ( RHeigth <= skipvalue < Neighbour2 ) OR
  642.       ( RHeigth <= skipvalue < Neighbour3 ) OR
  643.       ( RHeigth <= skipvalue < Neighbour4 )    THEN
  644.     skip := TRUE ;
  645.    END ;
  646.    RETURN skip ;
  647.   END skip_plain ;
  648.  (*-------------------------------------------------------------------------*)
  649.  
  650.  BEGIN                                   
  651.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  652.    fix    := Heigth > 0 ;
  653.    RHeigth := FLOAT( Heigth ) ;
  654.    previous := RHeigth ;                     
  655.  
  656.    REPEAT
  657.     medium := Average( RHeigth , 1 ) ;               (* 3x3 pixel size area *)
  658.     IF ( NOT fix ) THEN                              (* all free pixels     *)
  659.      RHeigth := medium ;
  660.     END ; (* IF ( NOT fix ) AND *)
  661.     change := ABS( RHeigth-previous) > 0.2 ;
  662.     previous := RHeigth ;
  663.     num_changes := REDUCE.SUM( ORD(change) ) ;
  664.     WriteString("Changes : ") ; WriteInt( num_changes , 3 ) ; WriteLn ;
  665.    UNTIL num_changes < 10 ;
  666.  
  667.    WriteString("Smoothing lines ...") ; WriteLn ;
  668.    FOR j := 1 TO 5 DO
  669.     RHeigth := Average(RHeigth,1) ;           (* take average of all pixels *)
  670.    END ; (* FOR *)
  671.  
  672.    WriteString("Computing heigth-lines ...") ; WriteLn ;
  673.    min_Value := REDUCE.MIN(RHeigth) ;
  674.    max_Value := REDUCE.MAX(RHeigth) ;
  675.    IF Batch THEN
  676.      num_Linien := Number_of_Lines ;
  677.     ELSE                                      
  678.      WriteString( "Minimum(Heigth) = ") ; 
  679.      WriteFixPt(min_Value,2,2) ;
  680.      WriteString( " , Maximum(Heigth) = ") ; 
  681.      WriteFixPt(max_Value,2,2) ; WriteLn ;
  682.      WriteString( "Number of heigth-lines you want to see : ");
  683.      ReadInt( num_Linien ) ;
  684.    END ;
  685.    step := (max_Value - min_Value) / FLOAT(num_Linien) ;
  686.  
  687.    Heigth := 0 ;
  688.    WHILE min_Value < max_Value DO
  689.     IF skip_plain( min_Value ) THEN
  690.      Heigth := maxDepth ; (* original : 16 *)
  691.     END ; (* IF *)
  692.     min_Value := min_Value + step ;
  693.    END ; (* WHILE *)
  694.   ENDPARALLEL ;
  695.  END Interpolate_and_Heigthlines ;
  696.                  
  697. (************************* find_vertical_rel_region *************************)
  698.  
  699. PROCEDURE find_vertical_rel_region ;  
  700.  CONST
  701.    relocationen = 5 ;
  702.  
  703.  SCALAR
  704.    quality : ARRAY[-relocationen .. relocationen],[-relocationen .. relocationen] OF INTEGER ;
  705.    i,j   : INTEGER ;
  706.  
  707.  VECTOR
  708.    Buffer : INTEGER ;
  709.  
  710.  BEGIN             
  711.   WriteString("Computing correction-value for vertical image-position ..."); 
  712.   WriteLn ;
  713.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  714.    FOR i := -relocationen TO relocationen DO
  715.     Buffer := Left ;
  716.     PROPAGATE .right_port^relocationen( Buffer ) ;
  717.     IF i # 0 THEN
  718.      IF i < 0 THEN 
  719.        j := -i ;
  720.        PROPAGATE .up_port^j( Buffer ) ;
  721.       ELSE
  722.        PROPAGATE .down_port^i( Buffer ) ;
  723.      END ;
  724.     END ;
  725.     FOR j := -relocationen TO relocationen DO
  726.      PROPAGATE .left_port( Buffer ) ;
  727.      IF ( 5 < DIM1 < PicHeigth-5 ) AND ( 5 < DIM2 < PicWidth-5 ) THEN
  728.       quality[i][j] := REDUCE.SUM( ABS( Buffer-Right )) ;
  729.      END ;
  730.     END ;
  731.    END ;  
  732.   ENDPARALLEL ;
  733.  
  734.   FOR i := -relocationen TO relocationen DO
  735.    FOR j := -relocationen+1 TO relocationen DO
  736.     quality[i][-relocationen] := quality[i][-relocationen] + quality[i][j] ;
  737.    END ;
  738.   END ;  
  739.   
  740.   j := -relocationen ;
  741.   FOR i := -relocationen+1 TO relocationen DO
  742.    IF quality[i][-relocationen] < quality[-relocationen][-relocationen] THEN 
  743.     quality[-relocationen][-relocationen] := quality[i][-relocationen] ;
  744.     j := i ;
  745.    END ;
  746.   END ;  
  747.   WriteString("Minimum in line ") ; WriteInt(j,2) ; WriteLn ;
  748.  END find_vertical_rel_region ;
  749.  
  750. (************************* main program *************************************)
  751.  
  752. BEGIN                                                         
  753.  (* initialize some variables *)
  754.  PARALLEL
  755.   FOR counter := 1 TO maxDepth DO
  756.    Pixel[counter] := 0 ;
  757.   END ;
  758.   Heigth := 0 ;
  759.   Left  := 0 ;
  760.   Right := 0 ;
  761.  ENDPARALLEL ;
  762.  STORE( Left , LeftPicture ) ;
  763.  STORE( Right , RightPicture ) ;
  764.  
  765.  counter := 1 ;                
  766.  
  767.  (* test, if batch-file exist *)
  768.  
  769.  OpenInput( "realpic.batch" ) ;
  770.  
  771.  IF Done THEN 
  772.    WriteString(" Program in Batch-Mode ...") ; WriteLn ;
  773.    Batch := TRUE ;
  774.   ELSE
  775.    Batch := FALSE ;
  776.  END ;
  777.  CloseInput ;
  778.  
  779.  LOOP
  780.   IF Batch THEN
  781.    OpenInput( "realpic.batch" ) ;
  782.    FOR i := 1 TO counter DO
  783.     ReadString( InputFile ) ;
  784.     ReadString( OutputFile ) ;
  785.     ReadInt( RegionX ) ;
  786.     ReadInt( RegionY ) ;             
  787.     ReadInt( Number_of_Lines ) ; 
  788.    END ; (* FOR i := 1 *)
  789.  
  790.    INC( counter ) ;
  791.  
  792.    IF NOT Done THEN    
  793.     WriteString( " ********* END ******** " ) ; WriteLn ;
  794.     HALT ;
  795.    END ; (* IF NOT Done *)
  796.  
  797.   END ; (* IF Batch *)
  798.   CloseInput ;
  799.  
  800.   IF ReadPicture() = FALSE THEN
  801.    WriteString("###### Program terminated abnormaly ######" ) ; 
  802.    WriteLn ; HALT ;
  803.   END ;       
  804.        
  805.   CloseInput ;                       (* return to input from console *)
  806.  
  807.   LOAD( Left  , LeftPicture  ) ;
  808.   LOAD( Right , RightPicture ) ; 
  809.  
  810.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  811.    Left  := Search_Edge( Left  ) ;
  812.    Heigth := Left ;
  813.   ENDPARALLEL ;
  814.   Show_All ;
  815.  
  816.   PARALLEL [ 1 .. PicHeigth ] , [ 1 .. PicWidth ]
  817.    Right := Search_Edge( Right ) ;
  818.    Heigth := Right ;
  819.   ENDPARALLEL ;
  820.   Show_All ;
  821.  
  822.   WriteString( "Examine left and right image for common elements ...") ; 
  823.   WriteLn ;
  824.  
  825.   Common_Elements ;
  826.   Show_Plains ;
  827.  
  828.   WriteString("Discovering objects in each plain ...") ; WriteLn ;
  829.                                          
  830.   Search_Objects ;                   (* find for each pixel the right plain *)
  831.                                          
  832.   Compute_Heigth ;
  833.   Show_Plains ;
  834.   Show_All  ;
  835.  
  836.   Filter ;
  837.   Compute_Heigth ;
  838.   Show_Plains ;
  839.   Show_All  ;
  840.  
  841.   (* saving resulting image in PPM-format *)
  842.  
  843.   IF NOT Batch THEN
  844.    WriteString( "Outputfile (without extension '.h.ppm' und '.f.ppm' ) : " ) ;
  845.                                      (* requesting filename  *)
  846.    ReadString( OutputFile ) ;
  847.   END ;
  848.  
  849.   FileName := strcat( OutputFile , ".f.ppm" ) ;
  850.   WriteString( "Saving resulting image as '") ; 
  851.   WriteString( FileName ) ;
  852.   WriteString("' ..." ) ; WriteLn ;
  853.  
  854.   PPM_Output ;                           
  855.  
  856.   WriteString("Interpolating and computing heigth-lines ...") ; WriteLn ;
  857.   Interpolate_and_Heigthlines ;                      
  858.   Show_All  ;
  859.  
  860.   FileName := strcat( OutputFile , ".h.ppm" ) ;
  861.   WriteString( "Saving interpolated image as '") ; 
  862.   WriteString( FileName ) ; 
  863.   WriteString("' ..." ) ; WriteLn ;
  864.  
  865.   PPM_Output ;
  866.                                         
  867.   IF Batch THEN
  868.     CloseOutput ; 
  869.    ELSE
  870.     HALT ;
  871.   END ;
  872.  
  873.  END (* LOOP *)
  874.  
  875. END Evaluation_of_real_Random_Stereo_Images .
  876.  
  877.